package com.wenan.data
{
	import com.wenan.ui.controls.flashMaps.MapEvent;
	
	import flash.display.GraphicsPathWinding;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.Alert;

	public class Province
	{
		private var _id:String;
		private var _name:String;
		private var _points:Array;
		private var _sprite:Sprite = null;
		
		public function Province()
		{
		}

		public function get id():String
		{
			return _id;
		}

		public function set id(value:String):void
		{
			_id = value;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function set name(value:String):void
		{
			_name = value;
		}

		public function get points():Array
		{
			return _points;
		}

		public function set points(value:Array):void
		{
			_points = value;
			
			invalidatePolygon();
		}

		public function get sprite():Sprite
		{
			if (_sprite == null)
			{
				_sprite = new Sprite();
				_sprite.alpha=0;
				_sprite.addEventListener(MouseEvent.ROLL_OVER,roll_over);
				_sprite.addEventListener(MouseEvent.ROLL_OUT,roll_out);
				_sprite.addEventListener(MouseEvent.CLICK,click_handler);
				invalidatePolygon();
			}
			return _sprite;
		}
		
		private function invalidatePolygon():void
		{
			if (_sprite && _points)
			{
				var pointNum:Number=points.length;
				var commands:Vector.<int>=new Vector.<int>();
				var data:Vector.<Number> = new Vector.<Number>();
				
				//绘制个多边形。
				for(var i:int=0;i<pointNum;i++)
				{
					if(i==0)
					{
						commands.push(1);
					}else
					{
						commands.push(2);
					}
					var point:Point=points[i] as Point;
					data.push(point.x,point.y);
				}
				
				_sprite.graphics.beginFill(0xFFD700,0.5);
				_sprite.graphics.drawPath(commands,data,GraphicsPathWinding.NON_ZERO);	
			}
			
		}
		
		private function roll_over(event:MouseEvent):void
		{
			_sprite.alpha=1;
		}
		
		private function roll_out(event:MouseEvent):void
		{
			_sprite.alpha=0;
		}
		
		private function click_handler(event:MouseEvent):void
		{
			var data:Object=new Object();
			data.province=this;
			data.x=event.localX;
			data.y=event.localY;
			
			_sprite.dispatchEvent(new MapEvent(MapEvent.CLICK_PROVINCE,data));
		}
	}
}